package controllers

import (
	"github.com/gin-gonic/gin"
	"github.com/globalsign/mgo/bson"

	"cmt1-admin/models"
)

type FireLogQuery struct {
	BaseRequest
	Uid       string `form:"uid"`
	Name      string `form:"name"`
	TimeOrder int    `form:"timeOrder"`
	StartTime int64  `form:"startTime"`
	EndTime   int64  `form:"endTime"`
}

func GetFireLogList(c *gin.Context) {
	q := &FireLogQuery{}
	if err := c.ShouldBindQuery(&q); err != nil {
		handleErr(c, 404, err.Error())
		return
	}

	mquery := make(map[string]interface{}, 0)
	if q.Name != "" {
		u := new(models.GameUser)
		err := GameDB.FindOne("user", bson.M{"name": q.Name}, bson.M{"_id": 1}, u)
		if err != nil {
			handleErr(c, 404, err.Error())
			return
		}
		mquery["uid"] = u.Uid.Hex()
	}
	if q.Uid != "" {
		mquery["uid"] = q.Uid
	}
	orderList := []string{"-time"}
	if q.TimeOrder == 1 {
		orderList = []string{"time"}
	}

	if q.StartTime > 0 && q.EndTime > 0 {
		mquery["time"] = bson.M{"$gte": q.StartTime, "$lte": q.EndTime}
	}

	count, err := GameLogDB.Count("FireLog", mquery)
	if err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	logList := make([]interface{}, 0)
	err = GameLogDB.FindPageSort("FireLog", q.Page-1, q.PageNum, mquery, nil, orderList, &logList)
	if err != nil {
		handleErr(c, 404, err.Error())
		return
	}
	handleOk(c, map[string]interface{}{"logList": logList, "count": count})
}
